package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Api(tags = "店铺操作接口")
@Slf4j
public class ShopController {

    @Autowired
    private RedisTemplate redisTemplate;

    // 店铺营业状态的 key
    private static final String key = "SHOP_STATUS";

    /**
     * 获取营业状态
     *
     * @return
     */
    @GetMapping("/status")
    @ApiOperation("获取营业状态")
    public Result<Integer> getShopStatus() {
        // 1. 从 Redis 中获取营业状态
        Integer status = (Integer) redisTemplate.opsForValue().get(key);
        log.info("获取营业状态：{}", status == 1 ? "营业中" : "打烊中");

        // 2. 返回数据
        return Result.success(status);
    }

    /**
     * 设置营业状态
     *
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation("设置营业状态")
    public Result<String> setShopStatus(@PathVariable("status") Integer status) {
        log.info("设置营业状态：{}", status == 1 ? "营业中" : "打烊中");
        // 1. 校验参数
        if (status != 0 && status != 1) {
            return Result.error("参数错误");
        }
        // 2. 修改店铺状态
        redisTemplate.opsForValue().set(key, status);

        // 3. 返回数据
        return Result.success();
    }
}
